home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / EnterAct 3.5 / Drag_on Modules / hAWK programs / $FrequencyWord < prev    next >
Encoding:
Text File  |  1993-04-09  |  2.5 KB  |  68 lines  |  [TEXT/KEEN]

  1. #$FrequencyWord: print sorted list of words, together with number
  2. #of times each word occurs. Use with any input option, 
  3. #select "Show stdout" since that's where the output goes.
  4. #This program prints the words in order by frequency of occurrence,
  5. #whereas $WordFrequency prints them alphabetically.
  6. #The file "common words" in the "hAWK programs" folder contains
  7. #a list of words to skip. To do a better job, you can create a
  8. #custom list - for example, the word "while" can be skipped in
  9. #ordinary text, but should be included if the text deals with
  10. #C or hAWK programming. If this file is missing, the program
  11. #will still run, but common words will not be skipped (this
  12. #uses more memory, and runs slower).
  13. #Tech note: the words in “common words” are loaded into the
  14. #(associative) array “common[]”; as with all arrays in hAWK,
  15. #retrieval of an element is done with a hash table, so retrieval
  16. #of an element given the index or checking for the existence
  17. #of an index with the “in” operator is very fast. Thus there would
  18. #be no real advantage to keeping the common words in alphabetical
  19. #order. Also, duplicate words cause no problems.
  20.  
  21. #This isn't perfect, but is very useful as-is. It's a simple
  22. #program, one you can tinker with easily - try it out on
  23. #some small files, and refinements will suggest themselves.
  24.  
  25. # User’s Manual references:
  26. # «hAWK User’s Manual» «F   Running hAWK programs»
  27. # «hAWK User’s Manual» «L  5   Regular expressions»
  28. # «hAWK User’s Manual» «M  5   Built-in string and file functions»
  29. # «hAWK User’s Manual» «K  4   Built-in variables»
  30. # «hAWK User’s Manual» «K  8   Arrays»
  31. # «hAWK User’s Manual» «N   User-defined functions»
  32. # «hAWK User’s Manual» «P  3   The getline function»
  33. # «hAWK User’s Manual» «O  3   Output into files»
  34. # «hAWK User’s Manual» «Q   The hAWK function»
  35.  
  36. BEGIN { #Get list of common words to skip.
  37.     commonfile = STDPATH "Drag_on Modules:hAWK programs:" "common words"
  38.     while (getline < commonfile > 0)
  39.         {
  40.         for ( k = 1; k <= NF; k++)
  41.             common[$k] = 1; #Forces common[$k] to "exist".
  42.         }
  43.     close(commonfile)
  44.     $0 = ""
  45.     ## time_it = 1
  46.     if (time_it == 1)
  47.         print "Starting time", time()
  48.     }
  49.  
  50.  
  51.     { #Remove non-word characters, count words.
  52.       gsub(/[^A-Za-z_0-9$'-]+/, " ")
  53.       #or try gsub(/\W+/, " ") #W == [^A-Z_a-z0-9]
  54.       for ( k = 1; k <= NF; k++)
  55.           {
  56.         if (length($k) > 1 && !($k in common))
  57.               count[$k]++;
  58.         }
  59.     }
  60. END { #Sort associative array, and print words with count.
  61.       m = sort(count, ind, "rn")
  62.       for (j = 1; j <= m; ++j)
  63.           print ind[j], "\t\t", count[ind[j]]
  64.       if (time_it == 1)
  65.         print "Finishing time", time()
  66.     }
  67.  
  68.